package checkers;

import java.util.List;
import java.util.Scanner;

public class CheckersConsoleGUI extends CheckersGUI {

    public static final char SEPARATOR_CHAR = '|';

    @Override
    public void showBoard(CheckersState checkersState) {
        System.out.flush();
        System.out.println("\nVez do jogador de peças " + (checkersState.getCurrentPlayer() == CheckersState.WHITE_PLAYER ? "brancas" : "pretas") + " jogar\n");
        byte[][] board = checkersState.getBoard();
        System.out.println("   1 2 3 4 5 6 7 8");
        for (int i = 0; i < board.length; i++) {
            System.out.print((char) ('A' + i));
            System.out.print(' ');
            for (int j = 0; j < board.length; j++) {
                System.out.print(SEPARATOR_CHAR);
                System.out.print((board[i][j] == 0 ? " " : String.valueOf(board[i][j])));
            }
            System.out.println(SEPARATOR_CHAR);
        }
        System.out.println();
    }

    public static void showBoard2(CheckersState checkersState) {
        System.out.flush();
        System.out.println("\nVez do jogador de peças " + (checkersState.getCurrentPlayer() == CheckersState.WHITE_PLAYER ? "brancas" : "pretas") + " jogar\n");
        byte[][] board = checkersState.getBoard();
        System.out.println("   1 2 3 4 5 6 7 8");
        for (int i = 0; i < board.length; i++) {
            System.out.print((char) ('A' + i));
            System.out.print(' ');
            for (int j = 0; j < board.length; j++) {
                System.out.print(SEPARATOR_CHAR);
                System.out.print((board[i][j] == 0 ? " " : String.valueOf(board[i][j])));
            }
            System.out.println(SEPARATOR_CHAR);
        }
        System.out.println();
    }

    @Override
    public void requestUserMovement(List<CheckersMovement> avaiableMovements) {
        boolean valid = false;
        do {
            System.out.print("Informe a posição inicial da peça que deseja movimentar: ");
            String sourcePos = new Scanner(System.in).nextLine().trim().toUpperCase();
            if (sourcePos.length() == 2) {
                byte sourceX = (byte) (sourcePos.charAt(0) - 'A');
                byte sourceY = (byte) (sourcePos.charAt(1) - '1');
                for (int i = 0; i < avaiableMovements.size(); i++) {
                    if (avaiableMovements.get(i).getFirst(CheckersOperation.SOURCE_POSITION).equalsPosition(sourceX, sourceY)) {
                        System.out.print("Informe a posição final da peça que deseja movimentar: ");
                        String targetPos = new Scanner(System.in).nextLine().trim().toUpperCase();
                        if (targetPos.length() == 2) {
                            byte targetX = (byte) (targetPos.charAt(0) - 'A');
                            byte targetY = (byte) (targetPos.charAt(1) - '1');
                            for (int j = i; j < avaiableMovements.size(); j++) {
                                if (avaiableMovements.get(j).getFirst(CheckersOperation.SOURCE_POSITION).equalsPosition(sourceX, sourceY)
                                        && avaiableMovements.get(j).getLast(CheckersOperation.TARGET_POSITION).equalsPosition(targetX, targetY)) {
                                    valid = true;
                                    lastUserChosenMovement = avaiableMovements.get(j);
                                    notificationFromGUI(CheckersGUI.NOTIFY_USER_HAS_CHOOSED_MOVEMENT);
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        } while (!valid);
    }

    @Override
    public void showMovement(CheckersMovement bestMovement) {
        //TODO
    }

    @Override
    public void showAIProcessing(boolean aiProcessing) {
        //TODO
    }

    @Override
    public void showGameEndedMessage(byte playerWhoWon) {
        //TODO
    }
}
